const path = require('path')
// giz压缩依赖
const CompressionPlugin = require('compression-webpack-plugin')
const isProd = process.env.NODE_ENV === 'production'
// 只有生产环境可以使用 contenthash
const hash = isProd ? 'contenthash' : 'hash'

function resolve(dir) {
  return path.join(__dirname, dir)
}
module.exports = {
  // 路径前缀
  publicPath: '/',
  lintOnSave: true,
  productionSourceMap: false,
  // transpileDependencies: ['yitu-module-general'],
  configureWebpack: config => {
    // 修改输出文件名
    config.output.filename = `[name].[${hash}].js`
    config.output.chunkFilename = `[name].[${hash}].js`
  },
  chainWebpack: (config) => {
    // 忽略的打包文件
    config.externals({
      'vue': 'Vue',
      'vue-router': 'VueRouter',
      'vuex': 'Vuex',
      // 'axios': 'axios',
      'element-ui': 'ELEMENT'
    })
    const entry = config.entry('app')
    entry.add('babel-polyfill').end()
    entry.add('classlist-polyfill').end()
    config
      .when(process.env.use_analyzer,
        config => {
          config.plugin('webpack-bundle-analyzer')
            .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
        }
      )

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config.plugin('compressionPlugin').use(new CompressionPlugin({
            test: /\.(js|css|sass)$/, // 匹配文件名
            threshold: 10240, // 对超过10k的数据压缩
            minRatio: 0.8,
            deleteOriginalAssets: false // 删除源文件
          }))
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                // yituModule: {
                //   name: 'chunk-yitu-module', // split elementUI into a single package
                //   priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                //   test: /[\\/]node_modules[\\/]_?yitu-module-general(.*)/ // in order to adapt to cnpm
                // },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'), // can customize your rules
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
          // config.optimization.runtimeChunk('single')
        }
      )
  },
  // configureWebpack: {
  //   // provide the app's title in webpack's name field, so that
  //   // it can be accessed in index.html to inject the correct title.
  //   devtool: 'source-map'
  // },
  devServer: {
    port: 8081,
    host: '0.0.0.0',
    proxy: {
      '/api': {
        // 本地服务接口地址
        target: 'http://192.9.102.59:15015/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/api': '/'
        }
      }
    }
  }
}
